/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;


import com.asseco.mio.BasicInfo;
import com.asseco.mio.DisciplineProcedure;
import com.asseco.mio.UserInstitution;
import com.asseco.mio.WorkPosition;
import com.asseco.mio.codex.DisciplineProcedureForm;
import com.asseco.mio.codex.MeasuresTaken;
import com.asseco.mio.codex.ResponsibilityReason;
import com.asseco.mio.codex.ResponsibilityType;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import com.asseco.mio.search.UserSearch;
import com.asseco.persistence.service.HibernateUtil;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.*;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author filip.apostolski
 */
@Controller
@RequestMapping("/searchDisciplineProcedures*")
public class SearchDisciplineProceduresController extends BaseFormController{

    private GenericManager<DisciplineProcedure, String> disciplineProcedureManager = null;
    private GenericManager<WorkPosition, String> workPositionManager = null;
    private GenericManager<BasicInfo, String> basicInfoManager = null;
    private GenericManager<ResponsibilityType, String> responsibilityTypeManager = null;
    private GenericManager<ResponsibilityReason, String> responsibilityReasonManager = null;
    private GenericManager<MeasuresTaken, String> measuresTakenManager = null;

    @Autowired
    public void setWorkPositionManager(@Qualifier("workPositionManager") GenericManager<WorkPosition, String> workPositionManager) {
        this.workPositionManager = workPositionManager;
    }

    @Autowired
    public void setBasicInfoManager(@Qualifier("basicInfoManager") GenericManager<BasicInfo, String> basicInfoManager) {
        this.basicInfoManager = basicInfoManager;
    }

    @Autowired
    public void setDisciplineProcedureManager(@Qualifier("disciplineProcedureManager") GenericManager<DisciplineProcedure, String> disciplineProcedureManager) {
        this.disciplineProcedureManager = disciplineProcedureManager;
    }

    @Autowired
    public void setResponsibilityTypeManagerManager(@Qualifier("responsibilityTypeManager") GenericManager<ResponsibilityType, String> responsibilityTypeManager) {
        this.responsibilityTypeManager = responsibilityTypeManager;
    }

    @Autowired
    public void setResponsibilityReasonManagerManager(@Qualifier("responsibilityReasonManager") GenericManager<ResponsibilityReason, String> responsibilityReasonManager) {
        this.responsibilityReasonManager = responsibilityReasonManager;
    }

    @Autowired
    public void setMeasuresTakenManagerManager(@Qualifier("measuresTakenManager") GenericManager<MeasuresTaken, String> measuresTakenManager) {
        this.measuresTakenManager = measuresTakenManager;
    }


    public List getDisciplineProcedure(DisciplineProcedure disciplineProcedure, User user) {
    if (disciplineProcedure==null){
        return null;
    }
    if (disciplineProcedure.getRespType().getId().equals("")&&disciplineProcedure.getMeasures().getId().equals("")&&disciplineProcedure.getRespReason().getId().equals("")&&disciplineProcedure.getWorkPosition().getEmployment().getFirstName().equals("")&&disciplineProcedure.getWorkPosition().getEmployment().getLastName().equals("")&&disciplineProcedure.getWorkPosition().getEmployment().getSocScrNo().equals("")&&disciplineProcedure.getWorkPosition().getPosition().equals("")&&disciplineProcedure.getProcDate()==null){
        return null;
    }
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String pamFirstName = "%";
        String pamLastName = "%";
        String pamSocScrNo = "%";
        String pamPosition = "%";
        String pamResponsibilityType = "%";
        String pamResponsibilityReason = "%";
        String pamMeasuresTaken = "%";
        String pamProcDate = "";

        if (disciplineProcedure.getWorkPosition().getEmployment().getFirstName()!=null){
            if (!disciplineProcedure.getWorkPosition().getEmployment().getFirstName().equals("")){
            pamFirstName = "%" + disciplineProcedure.getWorkPosition().getEmployment().getFirstName() + "%";
            }
        }
        if (disciplineProcedure.getWorkPosition().getEmployment().getLastName()!=null){
            if(!disciplineProcedure.getWorkPosition().getEmployment().getLastName().equals("")){
            pamLastName = "%" + disciplineProcedure.getWorkPosition().getEmployment().getLastName() + "%";
            }
        }
        if (disciplineProcedure.getWorkPosition().getEmployment().getSocScrNo()!=null ){
            if (!disciplineProcedure.getWorkPosition().getEmployment().getSocScrNo().equals("")){
            pamSocScrNo = "%" + disciplineProcedure.getWorkPosition().getEmployment().getSocScrNo() + "%";
            }
        }
        if (disciplineProcedure.getWorkPosition().getPosition()!=null ){
            if (!disciplineProcedure.getWorkPosition().getPosition().equals("")){
            pamPosition = "%" + disciplineProcedure.getWorkPosition().getPosition() + "%";
            }
        }
        if (disciplineProcedure.getRespType()!=null) {
            if(!disciplineProcedure.getRespType().getId().equals("")) {
                ResponsibilityType x = responsibilityTypeManager.get(disciplineProcedure.getRespType().getId());
                disciplineProcedure.setRespType(x);
                pamResponsibilityType = "%" + disciplineProcedure.getRespType().getName() + "%";
            }
        }
        if (disciplineProcedure.getRespReason()!=null) {
            if(!disciplineProcedure.getRespReason().getId().equals("")) {
                ResponsibilityReason x = responsibilityReasonManager.get(disciplineProcedure.getRespReason().getId());
                disciplineProcedure.setRespReason(x);
                pamResponsibilityReason = "%" + disciplineProcedure.getRespReason().getName() + "%";
            }
        }
        if (disciplineProcedure.getMeasures()!=null) {
            if(!disciplineProcedure.getMeasures().getId().equals("")) {
                MeasuresTaken x = measuresTakenManager.get(disciplineProcedure.getMeasures().getId());
                disciplineProcedure.setMeasures(x);
                pamMeasuresTaken = "%" + disciplineProcedure.getMeasures().getName() + "%";
            }
        }
        if (disciplineProcedure.getProcDate()!=null ){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(disciplineProcedure.getProcDate());
                pamProcDate = " dProcedure.procDate =  '" + date + "' and ";
        }

        Query q = session.createQuery("from DisciplineProcedure dProcedure where "
                  + "dProcedure.workPosition.institution in (:parameter) and ( "
                + "dProcedure.workPosition.employment.firstName like '" + pamFirstName + "' and "
                + "dProcedure.workPosition.employment.lastName like '" + pamLastName + "' and "
                + "dProcedure.workPosition.employment.socScrNo like '" + pamSocScrNo + "' and "
                + "dProcedure.workPosition.position like '" + pamPosition + "' and "
                + pamProcDate
                + "dProcedure.respType.name like '" + pamResponsibilityType + "' and "
                + "dProcedure.respReason.name like '" + pamResponsibilityReason + "' and "
                + "dProcedure.measures.name like '" + pamMeasuresTaken + "')");
        

        UserInstitution ui = new UserInstitution();
        List instList = ui.getInstitutionsForUser(user);
        q.setParameterList("parameter", instList);


        List tempResult = q.list();
        List result = new ArrayList();
        for (int j = 0; j < tempResult.size(); j++) {
            DisciplineProcedure object = (DisciplineProcedure) tempResult.get(j);
            result.add(object);
        }
        session.getTransaction().commit();
        return result;

    }

    @ModelAttribute
    public DisciplineProcedureForm disciplineProcedureForm() {
         DisciplineProcedureForm x = new DisciplineProcedureForm(null, null, this.responsibilityTypeManager.getAll(), this.responsibilityReasonManager.getAll(), this.measuresTakenManager.getAll());
         return x;
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView doSubmit(DisciplineProcedureForm disciplineProcedureForm, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

                String userString = request.getRemoteUser();
        User user = UserSearch.getUserByUserName(userString);

        DisciplineProcedureForm x = this.disciplineProcedureForm();
            if(disciplineProcedureForm.getDisciplineProcedure()!=null){
                x.setDisciplineProcedure(disciplineProcedureForm.getDisciplineProcedure());
            }
            else{
                x.setDisciplineProcedure(null);
            }
        return new ModelAndView("searchDisciplineProcedures", "searchDisciplineProceduresList", getDisciplineProcedure(x.getDisciplineProcedure(), user));
    }
    
     @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        super.initBinder(request, binder);
        SimpleDateFormat dateFormat =
                new SimpleDateFormat(getText("dd/MM/yyyy", request.getLocale()));
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
    }
}
